package com.yanq.liancar.activity;

import android.app.Dialog;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.RxBarTool;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.base.BaseToolbarActivity;
import com.yanq.liancar.bean.NoScrollViewPager;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.fragment.BHomeFragment;
import com.yanq.liancar.fragment.BMarketFragment;
import com.yanq.liancar.fragment.BMineFragment;
import com.yanq.liancar.util.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ServerMainActivity extends BaseToolbarActivity{

    @BindView(R.id.BviewPager)
    NoScrollViewPager viewPager;
    @BindView(R.id.Bhome)
    RadioButton Bhome;
    @BindView(R.id.Bmarket)
    RadioButton Bmarket;
    @BindView(R.id.Bmine)
    RadioButton Bmine;
    @BindView(R.id.BradioGroup)
    RadioGroup radioGroup;
    private List<Fragment> fragments = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_server_main);
        ButterKnife.bind(this);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);  //底部向上
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);//顶部向下
        if ("".equals(UserBean.getbToken(this)) || TextUtils.isEmpty(UserBean.getbToken(this))) {
            bottomDialog();
        }
        viewPager.setScroll(false);
        viewPager.setPageMargin(getResources().getDimensionPixelOffset(R.dimen.dp_5));
        viewPager.setOffscreenPageLimit(2);
        fragments.add(new BHomeFragment());
        fragments.add(new BMarketFragment());
        fragments.add(new BMineFragment());

        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }
        });

        setCurrent(0);


    }

    public void setCurrent(int id) {
        switch (id) {
            case 0:
                radioGroup.check(R.id.Bhome);
                //Utils.setStatusTextColor(true, this);
                break;
            case 1:
                radioGroup.check(R.id.Bmarket);
                //Utils.setStatusTextColor(true, this);
                break;
            case 2:
                radioGroup.check(R.id.Bmine);
                //Utils.setStatusTextColor(false, this);
                break;
        }

    }

    //登陆验证
    private void bottomDialog() {
        final Dialog bottomDialog = new Dialog(this, R.style.BottomDialog);
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_landing_verification, null);
        bottomDialog.setContentView(contentView);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels;
        //        params.bottomMargin = MyUtils.dp2px(this, 8f);
        contentView.setLayoutParams(params);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.getWindow().setGravity(Gravity.CENTER);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation2);
        bottomDialog.show();

        //确定
        TextView tv_confirm = bottomDialog.findViewById(R.id.submit);
        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RxActivityTool.skipActivity(ServerMainActivity.this, BLoginActivity.class);
                bottomDialog.dismiss();
            }
        });
        //取消
        TextView tv_cancle = bottomDialog.findViewById(R.id.cancel);
        tv_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomDialog.dismiss();
            }
        });
    }

    @OnClick({R.id.Bhome, R.id.Bmarket, R.id.Bmine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Bhome:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.Bmarket:
                viewPager.setCurrentItem(1, false);
                break;
            case R.id.Bmine:
                viewPager.setCurrentItem(2, false);
                break;
        }
    }

}